package com.zhisheng.myalipayTest;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import java.util.ArrayList;
import java.util.List;
/*
    从官网上直接复制的代码
 */
public class AlipayTradePagePay {

    public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        
        // 设置商户订单号
        model.setOutTradeNo("MyOrders:"+System.currentTimeMillis());
        
        // 设置订单总金额
        model.setTotalAmount("998");
        
        // 设置订单标题
        model.setSubject("沃尔玛自定义的测试");
        
        // 设置产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        
        // 设置PC扫码支付的方式
        model.setQrPayMode("1");
        
        // 设置商户自定义二维码宽度
        model.setQrcodeWidth(100L);
        
        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setShowUrl("https://tse3-mm.cn.bing.net/th/id/OIP-C.5AmeaglwRvHzzdbFVQcJ-QHaNK?rs=1&pid=ImgDetMain");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);
        
        // 设置订单绝对超时时间
        model.setTimeExpire("2026-12-31 10:05:01");

        
        // 设置请求后页面的集成方式
        model.setIntegrationType("PCWEB");

        
        // 设置商户门店编号
        model.setStoreId("NJ_00111111");
        
        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        
        request.setBizModel(model);

        // TODO 支付成功之后的同步回调地址
        // 支付成功之后的异步回调地址


        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCFq+dvce1T/YN3+2BawsstPMtuMWeW9Dvh3C0ONPwNxsAftnI8MtAUaEn2rkvk9ttavPqWxY4VA7Fz99aZL+scR/aZ5Twgbtm0jQrVocWktBLEcSrST7so0Jch2amouzEdgdv50sh/bFpRwLwPA8SeZmTIkKy2IlyqIRwHi+kGQcarlM0aF2WYSpm0l+4rTizkbgFrp/Mt+rwzmyT9VTA8Wb0hIIQhUzH8tQQlDT/INYnJGybzp4UlFuK4bxmUJiQTSp+li2zoMIMdluNha3MdsyTi/k3Na4qAZeBI+FggOamvyqITtWxRZyikmEE/sQCG3vHFIwCQPL5EDzZ7OtCDAgMBAAECggEATzxXM/iUdfUQuU6JD0PorCgdbnRas2CTHo8lJgbCYmgt+p5rmMOGFJK9RTyMQG0UGfVrlXQFhO9rgRKoYqXE+D6wLwlzWsZaxmvUwE3kmn0jos5yU7hveQIslCQyLZLIynTvsGcB8oaIA238C+pv4nVPyJP5zP0RcMevlcmZl9sJowhSFZ/m/a62WPf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IBCgKCAQEAjJOT/XOkqgvbapg5bbTRporPX3vyId/S4M7FxAledxnjj1ppa4KopJof3Bs1AuSFj2aHuHFzbu1OJnOAcgh835CRn13u/tkMZDineama9Wlchjsqc3kc893LG/XAsVFFymiHrT5HxGP+hCU6KzuaTnHfx6dJLGe0+0YUvpaOhBJD5yZUn0NZIwBy2XJFOTCoaNRLDUxAQCkU8EFVedpAmsD2MtnLqIkGLoFRG/8FD84Anb+ZZS6e4ThMhqKO5mnWmb7GnxzdEDmj2Y+FKkQqiLQ9FWeN2JfeEoMVZVjdOE5o2bLI0pzQrhZlfetjeqw0xxP7O+/P/F86wWfM6kpKHQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133680463");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}